## setwd("")
library(foreign)
library(readstata13)
library(dplyr)
library(grid)
library(ggplot2)

## Foreclosures Plot
require(gridExtra)

data.natl <- read.dta13("natl_foreclosures.dta")
plot1 <- ggplot(data.natl, aes(x = year, y = rate)) +
  geom_line() +
  ylab("Foreclosures Per 1,000 People") +
  xlab("") +
  ylim(0,20) +
  scale_x_continuous(breaks = c(2002,2006,2010,2014)) +
  annotate("text", x = 2009, y = 5, label = "USA")


data.state <- read.dta13("state_foreclosures.dta")
plot2 <- ggplot(data.state, aes(x = year, y = rate, group = state)) +
  geom_line() +
  ylab("") +
  xlab("") +
  ylim(0,20) +
  scale_x_continuous(breaks = c(2002,2006,2010,2014)) +
  annotate("text", x = 2009, y = 16.5, label = "NV") +
  annotate("text", x = 2010, y = 8.5, label = "AZ") +
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())

grid.arrange(plot1, plot2, ncol=2)
plot <- arrangeGrob(plot1, plot2, ncol=2)
ggsave(file="foreclosures.pdf", plot, height = 5, width = 9)

## CoreLogic foreclosure rate versus QCEW unemployment rate scatterplot

df <- read.dta13("foreclosure_unemployment.dta")
cor.coef <- round(cor(df$unemployment_rate, df$foreclosure_rate), digits = 3)

plot <- ggplot(df, aes(x = foreclosure_rate, y = unemployment_rate)) +
  geom_point(alpha = 0.25) +
  geom_smooth(method='lm', formula= y ~ x, se = FALSE, col = "black") +
  ylab("Unemployment Rate (QCEW)") +
  xlab("Foreclosures Per 1,000 People (CoreLogic)") +
  ylim(0,30) +
  xlim(0,20) +
  annotate("text", x = 17, y = 29, label = paste("Correlation Coefficient =",cor.coef)) 


plot
ggsave(file="corelogic_unemp_scatter.pdf", plot, height = 5, width = 7)

## Comparing Estimates to Healy and Lenz

results <- read.dta13("compare_results.dta")
results$lower <- results$b - 1.96*results$se
results$upper <- results$b + 1.96*results$se
results$x <- c(0.9,1.1,1.9,2.1)

plot <- ggplot(results, aes()) +
  xlim(0.5,2.5) +
  ylim(-2,4) +
  geom_segment(x = 0.9, xend = 0.9, y = results$lower[1], yend = results$upper[1], col = "gray40") +
  geom_segment(x = 1.1, xend = 1.1, y = results$lower[2], yend = results$upper[2], col = "gray40") +
  geom_segment(x = 1.9, xend = 1.9, y = results$lower[3], yend = results$upper[3], col = "gray40") +
  geom_segment(x = 2.1, xend = 2.1, y = results$lower[4], yend = results$upper[4], col = "gray40") +
  geom_point(aes(x = x[1], y = b[1], colour = "Foreclosures", shape = "Foreclosures", fill = "Foreclosures"), size = 4) +
  geom_point(aes(x = x[2], y = b[2], colour = "Healy-Lenz Index", shape = "Healy-Lenz Index", fill = "Healy-Lenz Index"), size = 4) +
  geom_point(aes(x = x[3], y = b[3], colour = "Foreclosures", shape = "Foreclosures", fill = "Foreclosures"), size = 4) +
  geom_point(aes(x = x[4], y = b[4], colour = "Healy-Lenz Index", shape = "Healy-Lenz Index", fill = "Healy-Lenz Index"), size = 4) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  scale_color_manual("", values = c("Foreclosures" = "black", "Healy-Lenz Index" = "black")) +
  scale_shape_manual("", values = c("Foreclosures" = 21, "Healy-Lenz Index" = 22)) +
  scale_fill_manual("", values = c("Foreclosures" = "salmon", "Healy-Lenz Index" = "cadetblue2")) +
  annotate("text", x = 1, y = 3.1, label = "Lagged DV") +
  annotate("text", x = 2, y = 1.3, label = "Diff-in-Diff") +
  labs(x = "", y = "Scaled Effect on Incumbent Vote Share") +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        legend.position=c(0.9, 0.9), 
        legend.background = element_rect(fill="transparent"), 
        legend.title=element_blank(),
        legend.key = element_blank())

plot
ggsave(file="compare.pdf", plot, height = 5, width = 7)

